<?php


namespace app\common\model;


use think\Model;

class UserCouponExchangeModel extends Model
{
    protected $name = 'user_coupon_exchange';



    /**
     * 个人中心--获取我的优惠券
     *
     * @param $user_id int 用户ID
     * @param $status int 状态:0=全部,1=未使用,2=已使用,3=已失效
     * @param $page int 页码
     * @param $limit int 每页数据
     */
    public function getUserCouponExchangePageList($params,$user_id,$page,$limit)
    {
        extract($params);

        try {
            $field = ['id','user_id','shop_id','order_id','goods_id','coupon_exchange_id','start_time','end_time','title','image','goods_num','claim_code','claim_code_image','claim_shop_id','status','createtime','use_time'];
            $where = [
                'user_id' => $user_id,
            ];
            if($status > 0){
                $where['status'] = $status;
            }
            $list = $this
                ->with(['shop' => function($query){
                        $query->field('id,shopname,image');
                        //$query->field('id,nickname as shopname,avatar as image');
                    }, 'goods' => function($query){
                        $query->field('id,goods_name,image');
                    }, 'claimshop' => function($query){
                        $query->field('id,shopname,image');
                        //$query->field('id,nickname as shopname,avatar as image');
                    }])
                ->field($field)
                ->where($where)
                ->order('createtime', 'desc')
                ->paginate($limit, false, ['page' => $page]);

            foreach ($list as $row){
                $row->start_time = datetime($row->start_time,'Y-m-d');
                $row->end_time = datetime($row->end_time,'Y-m-d');
                $row->createtime = datetime($row->createtime,'Y-m-d');
                $row->use_time = datetime($row->use_time,'Y-m-d');
                $row->image = full_url($row->image);
                $row->claim_code_image = full_url($row->claim_code_image);
                if($row->goods) {
                    $row->goods->image = full_url($row->goods->image);
                }
                if($row->shop) {
                    $row->shop->image = full_url($row->shop->image);
//                    $shop = model('app\common\model\ShopModel')->get(['user_id' => $row->user_id]);
//                    if($shop) {
//                        $user_shop = [
//                            'id' => $shop->user_id,
//                            'shopname' => $shop->shopname,
//                            'image' => full_url($shop->image),
//                        ];
//                        $row->shop = $user_shop;
//                    }
                }
                if($row->claimshop) {
                    $row->claimshop->image = full_url($row->claimshop->image);
//                    $shop = model('app\common\model\ShopModel')->get(['user_id' => $row->user_id]);
//                    if($shop) {
//                        $user_shop = [
//                            'id' => $shop->user_id,
//                            'shopname' => $shop->shopname,
//                            'image' => full_url($shop->image),
//                        ];
//                        $row->claimshop = $user_shop;
//                    }
                }
            }

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User','user_id','id');
    }


    public function shop()
    {
        //return $this->belongsTo('app\common\model\User','shop_id','id');
        return $this->belongsTo('app\common\model\ShopModel','shop_id','id');
    }


    public function claimshop()
    {
        //return $this->belongsTo('app\common\model\User','claim_shop_id','id');
        return $this->belongsTo('app\common\model\ShopModel','claim_shop_id','id');
    }


    public function goods()
    {
        return $this->belongsTo('app\common\model\goods\GoodsModel','goods_id','id');
    }


    public function coupon()
    {
        return $this->belongsTo('app\common\model\CouponExchangeModel','coupon_exchange_id','id');
    }


}